#ifndef PONG_BALL_H
#define PONG_BALL_H

#include <OgreMath.h>
#include <OgreFrameListener.h>
#include <OgreSceneNode.h>

#include <btBulletCollisionCommon.h>
#include <btBulletDynamicsCommon.h>

#include "Level.h"
#include "GameObj.h"

namespace pong {
    class Ball : public GameObject {
    protected:
		btDynamicsWorld* world;

    public:
        Ball(Core& c, Ogre::SceneNode* const node, const Level* const lvl,
			const Ogre::Real radius, btDynamicsWorld* const w)
            : GameObject(c, node, 0), world(w)
        {
            assert(radius > 0 && "Cannot create Ball with 0 radius!");
            
			Ogre::Entity* const b = node->getCreator()->createEntity("ball",
                                             Ogre::SceneManager::PT_SPHERE);
            renderNode->attachObject(b);
            const Ogre::Real scale = radius / b->getBoundingRadius();
            renderNode->scale(scale, scale, scale);

            btCollisionShape* fallShape = new btSphereShape(radius);
            btMotionState* fallMotionState =
new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
            btScalar mass = 1;
            btVector3 fallInertia(0,0,0);
            fallShape->calculateLocalInertia(mass,fallInertia);

            btRigidBody::btRigidBodyConstructionInfo
				fallRigidBodyCI(mass, fallMotionState, fallShape, fallInertia);
            body = new btRigidBody(fallRigidBodyCI);
            world->addRigidBody(body);
        }

        virtual ~Ball(void) {
			world->removeRigidBody(body);
			delete body->getMotionState();
            delete body;

			Ogre::SceneManager* const sceneMan = renderNode->getCreator();
			sceneMan->destroyMovableObject(renderNode->getAttachedObject(0));
        }
    };
}
#endif //PONG_BALL_H
